import {
	ref,
	reactive,
	onMounted,
	watchEffect
} from "vue";
const app = getApp()
export default function useAdressList(props) {
	const address = ref({})
	const chooseAddress = app.throttle(() => {
		console.log("选择地址")
		app.vibrate()
		uni.showLoading({
			title: '努力加载中',
			mask: true
		});
		if (false && app.globalData.addressList.find(p => p == app.globalData.appType)) {
			uni.chooseAddress({
				success(res) {
					console.log(res, '888888')
					if (res.resultStatus == "9000" || res.errMsg == "chooseAddress:ok") {
						const {
							provinceName,
							cityName,
							countyName,
							detailInfo,
							...others
						} = res
						address.value = {
							addressDetail: [provinceName, cityName, countyName, detailInfo]
								.toString().replace(/,/g, ""),
							...others
						}
					}
				},
				complete(res) {
					console.log(res, '5555555')
					uni.hideLoading()
				}
			})
		} else {
			uni.navigateTo({
				url: '/pages/addressList/addressList?type=1',
				// events: {
				// 	setAddress
				// },
				complete: () => {
					uni.hideLoading()
				}
			})
		}
	})
	const setAddress = (value) => {
		if (value) {
			address.value = {
				userName: value.name,
				telNumber: value.phone,
				addressDetail: [value.province, value.city, value.area, value.address].toString().replace(/,/g,
					""),
				...value
			}
		}
	}
	// 手机号做脱敏处理
	const phoneHide = (phone) => {
		let reg = /^(1[3-9][0-9])\d{4}(\d{4}$)/; // 定义手机号正则表达式
		phone = phone.replace(reg, '$1****$2');
		return phone; // 185****6696
	}
	const nameHide = (name) => {
		if (name.length == 2) {
			name = name.substring(0, 1) + '*'; // 截取name 字符串截取第一个字符，
			return name; // 张三显示为张*
		} else if (name.length == 3) {
			name = name.substring(0, 1) + '*' + name.substring(2, 3); // 截取第一个和第三个字符
			return name; // 李思思显示为李*思
		} else if (name.length > 3) {
			name = name.substring(0, 1) + '*' + '*' + name.substring(3, name.length); // 截取第一个和大于第4个字符
			return name; // 王五哈哈显示为王**哈
		} else {
			return name;
		}
	}
	onMounted(() => {
		setAddress(props)
	})
	return {
		address,
		chooseAddress,
		setAddress,
		phoneHide,
		nameHide,
	}
}